// pages/login/login.js
const util = require('../../utils/util.js');
const request = require('../../utils/request.js').request;
const app = getApp();


Page({

    formSubmit: function (e) {
        let that = this;
        if (this.data.mobile == "") {
            wx.showToast({
                title: '手机号不能为空',
                icon: '',
                image: '../../images/warn.png',
                duration: 2000,
                mask: true,
                success: function (res) {
                },
                fail: function (res) {
                },
                complete: function (res) {
                },
            })
            return;
        } else if (this.data.code == "") {
            wx.showToast({
                title: '验证码不能为空',
                icon: '',
                image: '../../images/warn.png',
                duration: 2000,
                mask: true,
                success: function (res) {
                },
                fail: function (res) {
                },
                complete: function (res) {
                },
            })
            return;
        } else {
            let params = {
                "mobile": this.data.mobile,
                "verificationCode": this.data.code,
                "sessionId": app.globalData.header
            };
            if (!(/^\d{6}$/.test(this.data.code))) {
                wx.showToast({
                    title: '动态码不正确',
                    icon: '',
                    image: '../../images/warn.png',
                    duration: 1000,
                    mask: true,
                    success: function (res) {
                    },
                    fail: function (res) {
                    },
                    complete: function (res) {
                    },
                })
                return false;
            }
            request('validate/code.json', params).then(function (res) {
                //console.log('aaa', res)
                if (res.data.success) {
                    app.globalData.login = true;
                    app.globalData.phone = that.data.mobile;
                    wx.navigateBack({
                        url: '../myinfo/myinfo',
                        success: function (res) {
                        },
                        fail: function (res) {
                        },
                        complete: function (res) {
                        },
                    })
                } else {
                    wx.showToast({
                        title: res.data.message,
                        icon: '',
                        image: '../../images/warn.png',
                        duration: 2000,
                        mask: true,
                        success: function (res) {
                        },
                        fail: function (res) {
                        },
                        complete: function (res) {
                        },
                    })
                }
            })
        }


    },
    getCode: function (e) {
        let that = this;
        if (that.data.mobile == "") {
            wx.showToast({
                title: '手机号不能为空',
                icon: '',
                image: '../../images/warn.png',
                duration: 2000,
                mask: true,
                success: function (res) {
                },
                fail: function (res) {
                },
                complete: function (res) {
                },
            })
            return;
        } else {
            if (!(/^1[34578]\d{9}$/.test(that.data.mobile))) {
                wx.showToast({
                    title: '手机号格式有误',
                    icon: '',
                    image: '../../images/warn.png',
                    duration: 1000,
                    mask: true,
                    success: function (res) {
                    },
                    fail: function (res) {
                    },
                    complete: function (res) {
                    },
                })
                return false;
            }
            let param = {
                "mobile": that.data.mobile
            };

            request('/verification/code.json', param).then(function (res) {
                console.log(res);
                that.setData({
                    codeDisabled: true
                });
                let i = 60;
                let countdown = setInterval(function () {
                    if (i > 0) {
                        that.setData({
                            codeBtnText: (i--) + '秒后重发'
                        })
                    } else {
                        that.setData({
                            codeDisabled: false,
                            codeBtnText: '获取动态码'
                        });
                        clearInterval(countdown);
                    }
                }, 1000);

            })
        }

    },
    mobileInput: function (e) {
        this.setData({
            mobile: e.detail.value
        })

    },
    codeInput: function (e) {
        this.setData({
            code: e.detail.value
        })

    },


    /**
     * 页面的初始数据
     */
    data: {
        mobile: '',
        code: '',
        openid: '',
        codeBtnText: '获取动态码',
        codeDisabled: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})